var MainPage_MAP = window.NameSpace || {};

var chinaMap_GeoCoord = {};

MainPage_MAP.createMap = function(divId) {
	var dom = document.getElementById(divId);
	MP_GL_MAP = echarts.init(dom);

	// 加载地图数据
	$.ajax({
		url : "/mainpage/loadAreaLable",
		async : false,
		success : function(data) {
			for (var i = 0; i < data.length; i++) {
				var value = [ data[i].lng, data[i].lat ];
				var key = data[i].city;
				chinaMap_GeoCoord[key] = value;
			}
			// console.log(JSON.stringify(chinaMap_GeoCoord));
		}
	});

	// 加载地图数据
	MainPage_MAP.loadData();
	
	return MP_GL_MAP;
}

MainPage_MAP.loadData = function() {
	

	var effect = {
		show : true,
		period : 50, // 运动周期，无单位，值越大越慢
		color : '#fff',
		shadowBlur : 5
	};

	var option = {
		tooltip : {
			trigger : 'item',
			formatter : '{b}',
			backgroundColor : '#000',
			textStyle : {
				color : '#fff'
			}
		},
		legend : {
			orient : 'vertical',
			x : 'left',
			y : 'bottom',
			selectedMode : 'multiple',
			textStyle : {
				color : '#333'
			}
		},
		series : []
	};

	$.ajax({
		url : "/mainpage/loadMapData",
		async : true,
		success : function(data) {
			MP_GL_MAP.showLoading();
			var legendColor = [];
			var legendData = [];
			var seriesData = [];
			for (var i = 0; i < data.length; i++) {
				// 设置legend颜色
				legendColor.push(data[i].color);
				legendData.push(data[i].name);

				var lineData = [];
				var lineList = data[i].lineList;

				for (var j = 0; j < lineList.length; j++) {
					var from = {};
					from.name = lineList[j].from_airport;
					var to = {};
					to.name = lineList[j].to_airport;

					lineData.push([ from, to ]);

					/*
					 * 有bug var point = {}; point.name = from.name; point.value =
					 * from.name ;
					 * 
					 * point.x = chinaMap_GeoCoord[point.name][0]; point.y =
					 * chinaMap_GeoCoord[point.name][1]; markPoints.push(point);
					 * 
					 * point.name = to.name; point.value = to.name ; point.x =
					 * chinaMap_GeoCoord[point.name][0]; point.y =
					 * chinaMap_GeoCoord[point.name][1]; markPoints.push(point);
					 */
					// console.log(point.x+" & "+point.y);
				}

				// itemColor
				var itemStyle = {
					normal : {
						color : '#fff',
						borderWidth : 1,
						borderColor : data[i].color,
						lineStyle : {
							type : 'solid'
						}
					}
				};

				// 设置series
				var series = {
					name : data[i].name,
					type : 'map',
					roam : false,
					hoverable : false,
					mapType : 'china',
					itemStyle : {
						normal : {
							borderColor : '#eee',
							borderWidth : 1,
							areaStyle : {
								color : '#3eaffd'
							}
						// ,label:{show:true}
						},
						emphasis : {
							label : {
								show : true
							}
						}
					},
					data : [],
					markLine : {
						symbol : [ 'arrow', 'arrow' ],
						symbolSize : 2,
						effect : effect,
						itemStyle : itemStyle,
						smooth : true,
						data : lineData
					}

				}
				if (i == 0) {
					series.geoCoord = chinaMap_GeoCoord;
				}
				seriesData.push(series);
			}

			option.color = legendColor;
			option.legend.data = legendData;
			option.series = seriesData;

			MP_GL_MAP.hideLoading();
			MP_GL_MAP.setOption(option, true);

			//var tmp = myChart;
			
			// 设置定时刷新任务
			setTimeout(MainPage_MAP.loadData, mp_refresh_time);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			console.log('exception>>'+textStatus);
			setTimeout(MainPage_MAP.loadData , mp_refresh_time);
		}
	});

}
